package com.uchonor.api.config;

import com.uchonor.olight.agent.module.web.vo.ServiceResult;
import org.springframework.stereotype.Component;
import org.ssssssss.magicapi.core.context.RequestEntity;
import org.ssssssss.magicapi.core.interceptor.ResultProvider;
import org.ssssssss.magicapi.modules.db.model.Page;

import java.util.List;
import java.util.Map;

@Component
public class CustomJsonValueProvider implements ResultProvider {
    /**
    *   定义返回结果，默认返回JsonBean
    */
	@Override
	public Object buildResult(RequestEntity requestEntity, int code, String message, Object data) {
		ServiceResult serviceResult = new ServiceResult();
		serviceResult.setSTATUS(String.valueOf(code));
		serviceResult.setMSG(message);
		serviceResult.setRSP(String.valueOf(code),message,data);
        // 如果对分页格式有要求的话，可以对data的类型进行判断，进而返回不同的格式
		return serviceResult;
	}

	/**
	 *   定义分页返回结果，该项会被封装在Json结果内，
	 *   此方法可以不覆盖，默认返回PageResult
	 */
	@Override
	public Object buildPageResult(RequestEntity requestEntity, Page page, long total, List<Map<String, Object>> data) {
		com.baomidou.mybatisplus.extension.plugins.pagination.Page resultPage = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>();
		resultPage.setRecords(data);
		resultPage.setCurrent(page.getOffset()/ page.getLimit()+1);
		resultPage.setSize(page.getLimit());
		resultPage.setTotal(total);
		ServiceResult serviceResult = new ServiceResult();
		serviceResult.setSTATUS("0000");
		serviceResult.setMSG("查询成功");
		serviceResult.setRSP("0000","查询成功",resultPage);
		return serviceResult;
	}
}